<?php
namespace Admin\Controller;
use Think\Controller;
/*
 * 社区党建————社区党建活动服务中心
 */
class ActivityController extends CommonController
{
    public function index()
    {
        $table = 'com_activity';
        $field = 'id,title,content,read_num,praise,show,update_time';
        $where = 'deletes = 1';
//        $activity = D('Street') -> dataInfo($table,$field,$where);
        if(!isset($_GET['p'])) {
            $_GET['p'] = 1;
        }

        // 实例化User对象
        $User = M($table);
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $activity = $User->field($field)->where($where)->order('id desc')->page($_GET['p'].',10')->select();
        foreach($activity as $key => $val){
            $report = M('report') -> field('report') -> where('tab_id = '.$val['id']) -> select();
            if($report){
                $activity[$key]['report'] = "有举报";
            }else{
                $activity[$key]['report'] = "未举报";
            }
        }
        $this->assign('activity',$activity);
        // 查询满足要求的总记录数
        $count      = $User->where($where)->count();

        // 实例化分页类 传入总记录数和每页显示的记录数
        $Page       = new \Think\Page($count,10);

        $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','共%TOTAL_PAGE%页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

        $show       = $Page->show();

        // 分页显示输出
        $this->assign('page',$show);
        $this -> display();
    }

    public function add()
    {
        $data = $_POST;
        if($data){
            $avtivity = M('com_activity');
            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] = date("Y-m-d H:i:s",time());
            if($data){
                $add = $avtivity  -> add($data);
                if($add){
                    $info['title'] ="恭喜您！";
                    $info['text'] = "添加成功！";
                    $info['type'] = "success";
                }else{
                    $info['title'] ="对不起！";
                    $info['text'] = "添加失败！";
                    $info['type'] = "error";
                }
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "添加失败，请重新填写信息！";
                $info['type'] = "error";
            }
            $this -> ajaxReturn($info);
        }
        $this->display();
    }

    public function upd()
    {
        $id = $_GET['id'];
        $data = $_POST;
        if(!empty($data)){
            $activ = M('com_activity');
            $data['update_time'] = date("Y-m-d H:i:s",time());
            if($data){
                $add = $activ ->where('id = '.$data['id'])-> field('title,content,show,update_time') -> save($data);
                if($add){
                    $info['title'] ="恭喜您！";
                    $info['text'] = "修改成功！";
                    $info['type'] = "success";
                }else{
                    $info['title'] ="对不起！";
                    $info['text'] = "修改失败！";
                    $info['type'] = "error";
                }
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "修改失败，请重新填写信息！";
                $info['type'] = "error";
            }
            $this -> ajaxReturn($info);
        }else{
            //文章表显示
            $table = 'com_activity';
            $field = 'id,title,content,show';
            $where = 'id = '.$id;
            $activ = M($table) -> field($field) -> where($where) -> find();
            $this->assign('activ',$activ );
            $this->display();
        }
    }

    public function del()
    {
        $id = I('post.id');
        $intro = M('com_activity');
        $data['deletes'] = 2;
        $rs = $intro -> where("id = $id") -> save($data);
        if($rs){
            $this->ajaxReturn("success");
        } else {
            $this->ajaxReturn("error");
        }
    }
}